<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
      <el-form-item label="借款额度">
        <el-input-number v-model="integralGrade.borrowAmount" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间开始">
        <el-input-number v-model="integralGrade.integralStart" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间结束">
        <el-input-number v-model="integralGrade.integralEnd" :min="0" />
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate()"> 保存 </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 引入api模块
import integralGradeApi from '@/api/core/integral-grade'
export default {
  data() {
    return {
      saveBtnDisabled: false, // 是否禁用保存按钮，防止表单重复提交
      integralGrade: {} // 积分等级对象
    }
  },
  // 页面渲染成功
  created() {
    // 当路由中存在id属性的时候，就是回显表单,需要调用回显数据的接口
    if (this.$route.params.id) {
      this.fetchById(this.$route.params.id)
    }
  },
  methods: {
    async fetchById(id) {
      const { data: res } = await integralGradeApi.getById(id)
      this.integralGrade = res.record
    },
    // 保存或更新
    saveOrUpdate() {
      // 禁用保存按钮
      this.saveBtnDisabled = true
      if (!this.integralGrade.id) {
        // 调用新增
        this.saveData()
      } else {
        // 调用更新
        this.updateData()
      }
    },
    async saveData() {
      const { message } = await integralGradeApi.save(this.integralGrade)
      // this.$message({
      //   type: 'success',
      //   message: message
      // })
      this.$message.success(message)
      this.$router.push('/core/integral-grade/list')
    },
    async updateData() {
      const { message } = await integralGradeApi.updateById(this.integralGrade)
      // this.$message({
      //   type: 'success',
      //   message: message
      // })
      this.$message.success(message)
      this.$router.push('/core/integral-grade/list')
    }
  }
}
</script>

<style scoped></style>
